<!--
-->
<template>
    <vxe-modal v-model="visible" :height="layout.modalHeight()" :min-width="layout.modalMinWidth(600)" :position="{top: '60px'}" :title="title" :width="layout.modalWidth(600)" destroy-on-close resize show-footer show-maximize @close="cancelClick">
        <el-form ref="ELForm" v-loading="isLoading" :model="state.model" :rules="rules" :validate-on-rule-change="false" class="ELForm" inline label-width="120px">
            <el-form-item :style="layout.col1" label="中文" prop="zh">
                <el-input v-model="state.model.zh" />
            </el-form-item>
            <el-form-item v-if="$hasPermission(permission.translate)" :style="layout.col1" label="英文" prop="en">
                <el-input v-model="state.model.en" style="width: calc(100% - 80px)" />
                <el-button @click="translate('en')" type="primary" plain><b-icon name="bi-translate" size="16" style="margin-right: 4px"/>翻译</el-button>
            </el-form-item>
            <el-form-item v-else :style="layout.col1" label="英文" prop="en">
                <el-input v-model="state.model.en" />
            </el-form-item>
            <el-form-item v-if="$hasPermission(permission.translate)" :style="layout.col1" label="俄语" prop="ru">
                <el-input v-model="state.model.ru" style="width: calc(100% - 80px)" />
                <el-button @click="translate('ru')" type="primary" plain><b-icon name="bi-translate" size="16" style="margin-right: 4px"/>翻译</el-button>
            </el-form-item>
            <el-form-item v-else :style="layout.col1" label="俄语" prop="ru">
                <el-input v-model="state.model.ru" />
            </el-form-item>
<!--            <el-form-item :style="layout.col1" label="开始版本" prop="startVersion">-->
<!--                <el-input-number v-model="state.model.startVersion" :min="0" controls-position="right" style="width: 100%;text-align: left;" />-->
<!--            </el-form-item>-->
<!--            <el-form-item :style="layout.col1" label="结束版本V" prop="endVersion">-->
<!--                <el-select v-model="state.model.endVersion" placeholder="0.0.0" style="width: 100%" filterable clearable>-->
<!--                    <el-option v-for="item in state.versionList" :key="item.id" :value="item.versionNum" :label="item.versionNum"/>-->
<!--                </el-select>-->
<!--            </el-form-item>-->
        </el-form>
        <template #footer>
            <el-button @click="cancelClick">取消</el-button>
            <el-button :loading="isSubmitting" type="primary" @click="beforeSubmit">
                确定
            </el-button>
        </template>
    </vxe-modal>
</template>

<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {FormMixins} from "@/utils/FormMixins";
import * as api from "./api.js";

const {ELForm, visible, title, layout, isLoading, isSubmitting, state, setOptions, submitSuccess, cancelClick} = FormMixins();
const {$message, $hasPermission} = window;

const permission = {
    translate: "ProgramManage:ProgramLanguage:TranslateContent"
}

const Model = () => {
    return {
        endVersion: null
    }
}

// 校验规则
const rules = computed(() => {
    return {
        programId: [{required: true, message: "请设置所属程序"}],
        zh: [{required: true, message: "请输入中文"}],
        // startVersion: [{required: true, message: "请输入开始版本"}],
    }
})

// 自定义校验规则
const customRules = {
    // pid: (rule, value, callback) => {
    //     if (state.model.type > 0) {
    //         if (!value) return callback(new Error("请选择上级菜单"));
    //     }
    //     return true;
    // },
};

async function onProgramChange() {
    const {data} = await api.GetVersionList({programId: state.model.programId});
    state.versionList = data || []
}

/**
 * 翻译内容
 * @param code
 * @returns {Promise<*>}
 */
async function translate(code) {
    if (!state.model.zh) return $message.warning("请设置中文内容")
    isLoading.value = true
    const {data} = await api.TranslateContent({content: state.model.zh, languageCode: code})
    isLoading.value = false
    if (data) {
        state.model[code] = data
        // const jsonRes = JSON.parse(data)
        // const transResults = jsonRes?.trans_result || []
        // state.model[code] = transResults[0]?.dst
    }
}

// 表单显示
async function show(config) {
    setOptions(config);
    state.model = Model();
    state.model = {...state.model, ...state.options}
    // 初始化数据
    api.GetProgramList({}).then(res => {
        state.programList = res.data || [];
    })
    onProgramChange()
}

// 校验表单
function beforeSubmit() {
    ELForm.value.validate(isOk => {
        isOk && submit()
    })
}

// 提交表单
async function submit() {
    isSubmitting.value = true
    console.log("submit: ", state.model)
    const body = {...state.model}
    const {success, data} = await api[state.model.id ? 'UpdateProgramLanguage' : 'AddProgramLanguage'](body);
    isSubmitting.value = false
    if (!success) return 0;
    $message.success("操作成功")
    submitSuccess()
}

onMounted(() => {
})
defineExpose({
    show
})
</script>


<style lang="scss" scoped>
.EditForm {

}
</style>
